import { defineStore } from 'pinia';

export const useEditorStore = defineStore('editor', {
    state: () => ({
        canvas: null,
        history: [],
        historyIndex: -1
    }),
    actions: {
        setCanvas(c) {
            this.canvas = c;
        },
        pushHistory() {
            const json = this.canvas.toJSON();
            this.history = this.history.slice(0, this.historyIndex + 1);
            this.history.push(json);
            this.historyIndex++;
        },
        undo() {
            if (this.historyIndex > 0) {
                this.historyIndex--;
                this.canvas.loadFromJSON(this.history[this.historyIndex], () => {
                    this.canvas.renderAll();
                });
            }
        }
    }
});
